var g_wakelock = null;
//允许程序后台运行，以持续获取GPS位置  
export function wakeLock() {
	//Android  
	var main = plus.android.runtimeMainActivity();
	var Context = plus.android.importClass("android.content.Context");
	var PowerManager = plus.android.importClass("android.os.PowerManager");
	var pm = main.getSystemService(Context.POWER_SERVICE);
	g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
	g_wakelock.acquire();
}
//结束程序后台运行  
export function releaseWakeLock() {
	if (g_wakelock != null && g_wakelock.isHeld()) {
		g_wakelock.release();
		g_wakelock = null;
	}
}
export function dataURLtoFile(dataurl, filename) {
	const arr = dataurl.split(',')
	const mime = arr[0].match(/:(.*?);/u)[1]
	const bstr = atob(arr[1])
	let n = bstr.length
	const u8arr = new Uint8Array(n)
	while (n--) {
		u8arr[n] = bstr.charCodeAt(n)
	}
	const blob = new Blob([u8arr], {
		type: mime
	})
	return new File([blob], filename, {
		type: mime,
		lastModified: new Date()
	})
}

export function base64ToBlob(data) {
    var arr = data.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
 
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}


//showToast 提示
export function showToast(msg) {
	console.log(msg, '152');
	uni.showToast({
		title: msg,
		icon: 'none'
	})
}
// 截取url中的code方法
export function getUrlCode() {
	var url = window.location.href;
	var theRequest = new Object()
	if (url.indexOf("?") != -1) {
		var str = url.split("?")[1]
		var strs = str.split("&");
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
		}
	}
	return theRequest
}


//封装复制到剪贴板的方法
export function copy(data) {
	uni.setClipboardData({
		data: data,
		success: (res) => {
			uni.showToast({
				title: '复制成功',
				icon: 'none',
				duration: 3000
			})
		}
	})
}
// 拨打电话
export function makePhoneCall(e) {
	uni.makePhoneCall({
		phoneNumber: e
	});
}
/*  
格式化时间戳
*/
export function timestampToTime(timestamp) {
	var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
	var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	return Y + M + D + h + m + s;
	// return Y + M + D+ h + m;
}

export function timestampToTime1(timestamp) {
	var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '.';
	var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
	var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
	var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	return M + D + h + m;
	// return Y + M + D+ h + m;
}


export function timestampToTime2(timestamp) {
	var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
	var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '日';
	var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
	var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	return M + D;
	// return Y + M + D+ h + m;
}
/* 
		验证邮箱
	 */
export function checkEmail(value) {
	return /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(value)
}
/**
 * 验证手机号
 * @param {Object} phone
 */
export function checkPhone(phone) {
	let zz = /^1[3456789]\d{9}$/;
	return zz.test(phone);
}
/**
 *手机掩码
 * @param {Object} phone
 */
export function phoneMask(phone) {
	return phone.substring(0, 3) + '****' + phone.substring(7);
}
/**
 *  页面返回
 */
export function backTo() {
	uni.navigateBack({
		delta: 1,
	})
}
// 限制字数
export function truncateText(text, length) {
	if (text.length <= length) {
		return text;
	}
	return text.substring(0, length) + '...';
}

// utils.js
export function base64ToTempPath(base64) {
  return new Promise((resolve, reject) => {
    // 去除 Base64 头部（如 "data:image/png;base64,"）
    const base64Data = base64.replace(/^data:image\/\w+;base64,/, '');
    // 获取文件管理器
    const fs = uni.getFileSystemManager();
    // 生成临时路径
    const tempFilePath = `${wx.env.USER_DATA_PATH}/temp_qrcode.png`;
	console.log(tempFilePath,'tempFilePath');
    // 写入文件
    fs.writeFile({
      filePath: tempFilePath,
      data: base64Data,
      encoding: 'base64',
      success: () => resolve(tempFilePath),
      fail: (err) => reject(err)
    });
  });
}
export function parsedEmojis (txt){
	const key = txt.replace(/[\[\]]/g, '');
	return emojiMap[key] || '❓'; // 无法识别时显示问号
}

